package main

import (
	"github.com/gin-gonic/gin"
	"k8sapi/core"
	"k8sapi/deployment"
	"k8sapi/lib"
	"net/http"
)

func main() {
      r:=gin.New()
      r.Use(func(context *gin.Context) {
		  defer func() {
		  	if e:=recover();e!=nil{
		  		context.AbortWithStatusJSON(400,gin.H{"error":e})
			}
		  }()
		  context.Next()
	  })
      //注册 deployment相关 handler函数
      deployment.RegHandlers(r)
      //静态资源
	  r.Static("/static", "./static")
      //加载静态模板
	  r.LoadHTMLGlob("html/**/*")
      // deployments模板路由
      r.GET("/deployments", func(c *gin.Context) {

		  c.HTML(http.StatusOK, "deployment_list.html",
		  		  lib.DataBuilder().
		  			SetTitle("deployment列表").
		  			SetData("DepList",deployment.ListAll("default")))
	  })
      // deployment详细的模板路由
	  r.GET("/deployments/:name", func(c *gin.Context) {
		c.HTML(http.StatusOK, "deployment_detail.html",
			lib.DataBuilder().
				SetTitle("deployment详细-"+c.Param("name")).
				SetData("DepDetail",deployment.GetDeployment("default",c.Param("name"))))
	})
	  //创建deployment的路由
	  r.GET("create/deployments", func(c *gin.Context) {
		  c.HTML(http.StatusOK,"deployment_create.html",
			  lib.DataBuilder().
			  SetTitle("deployment创建"))
	  })
      core.InitDeployment() //初始化deployment列表
      r.Run(":8080")

}
